package com.ciaj.boot.handle;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.log4j.Log4j2;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/***
 * @Author: ciaj.
 * @Description:
 * @Date: 15:26 2020/8/8
 **/
@Component
@Log4j2
public class MybatisObjectHandler implements MetaObjectHandler {
    static private String CERATE_BY = "system";

    @Override
    public void insertFill(MetaObject metaObject) {
        this.setInsertFieldValByName("createTime", LocalDateTime.now(), metaObject)
                .setInsertFieldValByName("createBy", CERATE_BY, metaObject)
                .setInsertFieldValByName("updateTime", LocalDateTime.now(), metaObject)
                .setInsertFieldValByName("updateBy", CERATE_BY, metaObject)
                .setInsertFieldValByName("del", 0, metaObject)
                .setInsertFieldValByName("version", 0, metaObject);

        log.debug("MybatisObjectHandler.insertFill ......................");
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        this.setInsertFieldValByName("updateTime", LocalDateTime.now(), metaObject);
        this.setInsertFieldValByName("updateBy", CERATE_BY, metaObject);

        log.debug("MybatisObjectHandler.updateFill ......................");
    }
}

